package com.escom.healthlc.security;

import com.escom.healthlc.entidad.MedicoVO;
import com.escom.healthlc.entidad.UsuarioVO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LifeCycleListener implements PhaseListener {

    private static final Logger logger = LoggerFactory.getLogger(LifeCycleListener.class);

    List<String> pagesMedico;
    List<String> pagesAdmin;
    List<String> pagesGeneral;

    public void afterPhase(PhaseEvent event) {
    }

    public void beforePhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        String currentPage = facesContext.getViewRoot().getViewId();
        pagesAvailable();
        HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(true);
        ExternalContext externalContext = facesContext.getExternalContext();
        String path = ((ServletContext) externalContext.getContext()).getContextPath();
        MedicoVO usuarioMedico = (MedicoVO) httpSession.getAttribute("usuarioActivio");
        UsuarioVO usuarioCurrent = null;
        if (usuarioMedico != null) {
            usuarioCurrent = usuarioMedico.getUsuario();
        } else {
            usuarioCurrent = new UsuarioVO();
            usuarioCurrent.setTipoUsuario("");
        }
        String tipoUsuario = usuarioCurrent.getTipoUsuario();
        logger.debug("Pagina actual: {}", currentPage);
//        logger.debug("tipoUsuario: {}", usuarioCurrent.toString());
        try {
            switch (tipoUsuario) {
                case "medico":
                    if (!pagesMedico.contains(currentPage)) {
                        externalContext.redirect(path + "/view/index-med.jsf");
                    }   break;
                case "admin":
                    if (!pagesAdmin.contains(currentPage)) {
                        externalContext.redirect(path + "/view/index-adm.jsf");
                    }   break;
                default:
                    if (!pagesGeneral.contains(currentPage)) {
                        if (!currentPage.equals("/index.xhtml")) {
                            externalContext.redirect(path + "/index.jsf");
                        }
                    }   break;
            }
        } catch (IOException e) {
            logger.error("Logger Error: {}", e.getCause().getMessage());
            logger.error("Logger Error: {}", e.fillInStackTrace().toString());
        }
    }

    public void pagesAvailable() {
        pagesMedico = new ArrayList<>();
        pagesAdmin = new ArrayList<>();
        pagesGeneral = new ArrayList<>();
        pagesMedico.add("/view/index-med.xhtml");
        pagesMedico.add("/view/med-paciente.xhtml");
        pagesMedico.add("/view/med-consulta.xhtml");
        pagesMedico.add("/view/med-historia-familiar.xhtml");
        pagesMedico.add("/view/med-cuenta-medico.xhtml");
        pagesMedico.add("/view/med-ecg.xhtml");
        pagesAdmin.add("/view/index-adm.xhtml");
        pagesAdmin.add("/view/adm-hospital.xhtml");
        pagesAdmin.add("/view/adm-parentesco.xhtml");
        pagesAdmin.add("/view/adm-enfermedad.xhtml");
        pagesAdmin.add("/view/adm-arritmia.xhtml");
        pagesAdmin.add("/view/adm-medico-mng.xhtml");
        pagesAdmin.add("/view/adm-rp.xhtml");
        pagesGeneral.add("/index.xhtml");
        pagesGeneral.add("/view/gr-registrar.xhtml");
        pagesGeneral.add("/view/gr-reportes.xhtml");
    }

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

}
